/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fait.ferm.persists;

import fait.ferm.global.Glob;
import fait.ferm.tools.Tools;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Greg
 */
public class Credit extends PersistsObject {
    public int bank_id;
    public int credit_type_id;
    public double amount_given;
    public double amount_received;
    public Date date_beg;
    public Date date_end;
    
    public Credit() {
        super();
        table = "credit";
        bank_id = 1;
        date_beg = Glob.getGame().current_date;
        amount_received = 0.0;
    }
    
    public Credit(int pId) throws Exception {
        super(pId);
        table = "credit";
        load();
    }
    
    @Override
    public void load() throws Exception {
        boolean notSuccessfully=true;
		if(id==-1) {
			throw new Exception("Объект "+table+" отсутствует в базе");
		}	
		try (Statement st = con.createStatement()) {
			String q = "SELECT ferm_id, bank_id, credit_type_id, amount_given, amount_received, date_beg, date_end FROM "+table+" WHERE id = " + id;
			ResultSet rs = st.executeQuery(q);
			if(rs.next()){
				notSuccessfully=false;
                if(rs.getInt("ferm_id") != Glob.getFerm().getId()) {
                    throw new Exception("Объект не из этой игры");
                }
                bank_id = rs.getInt("bank_id");
                credit_type_id = rs.getInt("credit_type_id");
                amount_given = rs.getDouble("amount_given");
                amount_received = rs.getDouble("amount_received");
                date_beg = Date.valueOf(rs.getString("date_beg"));
                date_end = Date.valueOf(rs.getString("date_end"));
			}
			rs.close();
		}
		if(notSuccessfully) {
			throw new Exception("Объект "+table+" не загружен");
        }
    }

    @Override
    protected void update() throws SQLException {
        System.out.println(date_beg.toString());
        try (Statement st = con.createStatement()) {
			String q = "UPDATE "+table+" SET " + 
                    "bank_id=" + bank_id + ", " +
                    "credit_type_id=" + credit_type_id + ", " +
                    "amount_given=" + amount_given + ", " +
                    "amount_received=" + amount_received + ", " +
                    "date_beg='" + date_beg.toString() + "', " +
                    "date_end='" + date_end.toString() + "' " +
					"WHERE id = " + id;
			st.executeUpdate(q);
		}
    }

    @Override
    protected void createNew() throws SQLException, Exception {
        try (Statement st = con.createStatement()) {
			String q = "INSERT INTO " + table + " (ferm_id, bank_id, credit_type_id, amount_given, amount_received, date_beg, date_end) VALUES("+
						Glob.getFerm().getId() + ", " +
                        bank_id + ", "+
                        credit_type_id + ", "+
						amount_given + ", "+
                        amount_received + ", "+
                        "'" + date_beg.toString() + "', "+
                        "'" + date_end.toString() + "'" +
					")";
			st.executeUpdate(q);
			q="SELECT seq FROM sqlite_sequence WHERE name = '"+table+"'";
			ResultSet rs = st.executeQuery(q);
			if(rs.next()){
				id = rs.getInt("seq");
			}
			rs.close();
		}
    }
    
}
